const db = require("../../../util/dbConfig/dbConfig")

module.exports = async function loadPaging (req,res,where,module) {
    let {page} = req.body
    //没有页码返回错误
    if (!page) {
        return res.send({
            code: 1,
            msg: '参数错误，请输入页码'
        })
    }
    if(where.length === 0){
        return res.send({
            code: 1,
            msg: '无参数'
        })
    }
    //有页码
    let newPage = (page - 1) * 10 //跳过多页
    //声明sql语句，准备建立查询
    let sySql
    let sqlArr = where
    if(module === 'baseInfo'){
        //如果该模块是基本信息baseInfo
        sySql=`select * from (
select 
a.ID 个体号,
a.VARIETY 品种,
e.name 户主,
e.id 身份证号,
case a.sex when 1 then '公羊' else '母羊' end as '性别',
a.BIRTHDAY 出生日期,
a.LEVEL 级别,
a.DIE 死亡,
a.DIE_DATE 死亡日期,
a.SOLD_ID 订单编号,
TIMESTAMPDIFF(year,a.birthday,now()) 年龄,
a.BIRTH_WEIGHT 出生体重,
a.WEANING_WEIGHT 断奶体重
from sheep a,user e
where  a.user_id=e.id) g
where g.个体号=?
or g.户主=? 
or g.品种=? 
or g.出生日期>=?
and g.出生日期<=?
or g.出生体重>=?
and g.出生体重<=?
or g.断奶体重>=?
and g.断奶体重<=?
and g.身份证号=${req.session.userInfo._id}
limit ${newPage},10`
    }
    else if(module === 'xiPuWord'){
        //如果是系谱档案模块
        sySql=`select * from (
select 
e.id 身份证号,
a.ID 个体号,
a.fu 父号,
a.mu 母号,
a.zufu 祖父号,
a.zumu 祖母号,
a.waizufu 外祖父号,
a.waizumu 外祖母号,
a.DIE 死亡,
a.DIE_DATE 死亡日期,
a.SOLD_ID 订单编号,
TIMESTAMPDIFF(year,a.birthday,now()) 年龄,
a.BIRTH_WEIGHT 出生体重,
a.WEANING_WEIGHT 断奶体重
from sheep a,user e
where a.user_id=e.id) g
where g.个体号=?
and g.身份证号=${req.session.userInfo._id}
limit ${newPage},10`
    }
    else if(module === 'TestRong'){
        //如果是绒质检测模块
        sySql=`select * from (
select 
a.DIE 死亡,
a.DIE_DATE 死亡日期,
a.SOLD_ID 订单编号,
e.id 身份证号,
a.ID 个体号,
c.YEAR 年度,
c.BODYTALL 体高,
c.BODYLONG 体长,
c.CHEST 胸围,
c.WEIGHT 体重,
c.LENGTH 绒长度,
c.FINE 绒细度,
c.RONGYIELD 绒产量,
c.PIPE 管围,
c.TANK 等级,
c.TINCK_WOOL 绒厚,
c.REMARK 备注
from sheep a,production c,user e
where a.id=c.sheep_id
and a.user_id=e.id) g
where g.个体号=?
or g.等级=?
or YEAR(g.年度)=?
or g.体高<=?
and g.体高>=?
or g.体长<=?
and g.体长>=?
or g.胸围<=?
and g.胸围>=?
or g.体重<=?
and g.体重>=?
or g.绒长度<=?
and g.绒长度>=?
or g.绒产量<=?
and g.绒产量>=?
or g.管围<=?
and g.管围>=?
or g.绒细度<=?
and g.绒细度>=?
or g.绒厚<=?
and g.绒厚>=?
and g.身份证号=${req.session.userInfo._id}
limit ${newPage},10`
    }

    if(req.session.userInfo.admin === 1){
        //管理员账号
        let index1 = sySql.lastIndexOf('and') //查找最后一位and的索引下标
        let sySql1 = sySql.substring(0,index1)    //substring(0,最后一位的索引下标值)，截取and之前的sql语句
        let index2 = sySql.lastIndexOf('limit') //查找最后一位LIMIT的索引下标
        let sySql2 = sySql.substring(index2)    //substring(最后一位的索引下标值)，截取LIMIT之后的sql语句
        sySql = sySql1+sySql2
    }
    console.log(sySql);
    let result = await db.SySqlConnect(sySql,sqlArr)
    if(result.length === 0){
        return res.send({
            code: 1,
            msg: '该页无数据'
        })
    }

    //查找总条数
    let countSql = `SELECT count(*) FROM  (${sySql}) `
    let index = countSql.lastIndexOf('limit') //查找最后一位LIMIT的索引下标
    countSql = countSql.substring(0,index)    //substring(0,最后一位的索引下标值)，截取LIMIT之前的sql语句
    countSql = countSql +')' +'g'    //为sql语句补充substring()删掉的 ')'  和   补充’g‘
    let countAll = await db.SySqlConnect(countSql,sqlArr)
    countAll = countAll[0]['count(*)']

    return res.send({
        code: 0,
        msg: '加载成功',
        data:{
            countAll,
            length: result.length,
            result
        }
    })

}
